package com.matt.domain.course;

import com.matt.commons.model.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 课程的扩展信息
 * 课程体系 (即 课程分类) type
 * 课程体系分类code : typeCode
 * 课程名称 name
 * 起始月龄 beginMonth
 * 截止月龄 endMonth
 * 适用对象名称(麦宝, 麦芽, 麦萌, 麦穗) targetObjectName
 * 适用对象id(麦宝, 麦芽, 麦萌, 麦穗) targetObjectId
 * 核心能力目标 (S, A, T, NULL) coreCompetence
 * 综合技能目标 (精细动作, 大肢体发展, 记忆力, 语言表达, 自我服务, 自我学习, 其他) integratedSkill
 * 课时 durationTime
 * 课程周数 week
 */
@Entity
@Table(name = "course_detail")
@Data
@EqualsAndHashCode(callSuper = true)
public class CourseDetail extends Model{

    @Column(columnDefinition = "varchar(32) COMMENT '课程id'")
    private String courseId;

    @Column(columnDefinition = "int default 0  COMMENT '课时'")
    private Integer durationTime = 0;

    @Column(columnDefinition = "varchar(32) COMMENT '适用对象id'")
    private String targetObjectId;

    @Column(columnDefinition = "varchar(32) COMMENT '适用对象名称'")
    private String targetObjectName;

    @Column(columnDefinition = "int default 0  COMMENT '起始月龄'")
    private Integer beginMonth = 0;

    @Column(columnDefinition = "int default 0   COMMENT '截止月龄'")
    private Integer endMonth = 0;

    @Column(columnDefinition = "varchar(50) COMMENT '核心能力目标' ")
    private String coreCompetence;

    @Column(columnDefinition = "varchar(255) COMMENT '综合技能目标' ")
    private String integratedSkill;

    @Column(columnDefinition = "varchar(500) COMMENT '视频地址'")
    private String videoUrl;

    @Column(columnDefinition = "varchar(500) COMMENT '视频转码地址'")
    private String videoSSUrl;

    public CourseDetail() {
    }

    public CourseDetail(String courseId, Integer durationTime, String targetObjectId, String targetObjectName, Integer beginMonth, Integer endMonth, String coreCompetence, String integratedSkill, String videoUrl, String videoSSUrl) {
        this.courseId = courseId;
        this.durationTime = durationTime;
        this.targetObjectId = targetObjectId;
        this.targetObjectName = targetObjectName;
        this.beginMonth = beginMonth;
        this.endMonth = endMonth;
        this.coreCompetence = coreCompetence;
        this.integratedSkill = integratedSkill;
        this.videoUrl = videoUrl;
        this.videoSSUrl = videoSSUrl;
    }

    //根据传入的字段 设置他的值
    public static CourseDetail setProperty(CourseDetail courseDetail, String filedCode, Object value) {
        Class clazz = courseDetail.getClass();
        String methodName = "set" + filedCode.substring(0, 1).toUpperCase() + filedCode.substring(1, filedCode.length());
        Method method = null;
        try {
            if (value instanceof String) {
                method = clazz.getMethod(methodName, String.class);
            }
            if (value instanceof Integer) {
                method = clazz.getMethod(methodName, Integer.class);
            }
            method.invoke(courseDetail, value);
            return courseDetail;
        } catch (Exception e) {
            e.printStackTrace();
            return courseDetail;
        }
    }

    //取全部字段名称
    public static Set<String> getAllFields() {
        Set<String> names = new HashSet<>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(CourseDetail.class);
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                names.add(property.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        names.removeAll(Arrays.asList("class","courseId","id"));
        return names;
    }

}
